package com.cg.asm.license.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * 服务器网络接口工具类
 */
public class ServerNetInterfaceUtils {

    /**
     * 获取本机的IPv4地址 （多个网络接口获取多个）
     */
    public static List<String> getServerIp4Address() {
        List<String> ipList = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();

            InetAddress ip;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();

                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }

                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = addresses.nextElement();
                    if (null != ip && ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() && !ip.isLinkLocalAddress() && ip.isSiteLocalAddress()) {
                        ipList.add(ip.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ipList;
    }

    /**
     * 获取服务器中,指定IP网卡的Mac地址
     */
    public static String getServerMacAddress(String authIP) {
        String serverMac = "";
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            byte[] mac;     // 网络接口的MAC地址
            InetAddress ip; // 网络接口的IP（包含IPv4 IPv6等）
            String ipv4 = "";    // 网络接口的IPv4地址
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }
                // 该网络接口的MAC地址
                mac = netInterface.getHardwareAddress();
                // 该网络接口的IP信息, 获取其IPv4地址
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = addresses.nextElement();
                    if (null != ip && ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() && !ip.isLinkLocalAddress() && ip.isSiteLocalAddress()) {
                        ipv4 = ip.getHostAddress();
                        break;
                    }
                }
                if (null != mac && authIP.equals(ipv4)) {
                    StringBuilder sb = new StringBuilder();
                    for (byte address : mac) {
                        String s = Integer.toHexString(address & 0xFF);
                        sb.append(s.length() == 1 ? "0" + s : s).append("-");
                    }
                    serverMac = sb.substring(0, sb.length() - 1).toUpperCase();
                    break;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return serverMac;
    }

    public static void main(String[] args) {
        System.out.println(getServerIp4Address());
        System.out.println("-------------------");
        System.out.println(getServerMacAddress("192.168.60.136"));
        System.out.println("-------------------");
    }

}
